package com.android.takegism.ui;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.android.takegism.R;
import com.android.takegism.utils.DbUtils;
import com.android.takegism.utils.MyContext;
import com.android.takegism.utils.MyDbOpenHelper;

public class LoginActivity extends AppCompatActivity implements View.OnClickListener {
    private MyContext myContext = MyContext.getInstance();
    private String defaultIp = "10.0.2.3";  //"192.168.43.211";
    private String defaultPort = "8888";
    private EditText ip_address;
    private EditText port;
    private RadioGroup identify;
    private Button login;
    private Button exit;
    private MyDbOpenHelper myDbOpenHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        myContext.setStatics("ip", defaultIp);
        myContext.setStatics("port", defaultPort);
        init();
    }

    private void init() {
        DbUtils instance = DbUtils.getInstance();
        instance.setContext(this,"my.db");

        login = findViewById(R.id.login_button);
        login.setOnClickListener(this);
        exit = findViewById(R.id.exit_button);
        exit.setOnClickListener(this);
        ip_address = findViewById(R.id.ip_address);
        ip_address.setText(defaultIp);
        port = findViewById(R.id.port);
        port.setText(defaultPort);
        identify = (RadioGroup) findViewById(R.id.identifier);
        identify.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                TextView p_tmp = findViewById(R.id.port_textView);
                switch (checkedId) {
                    case R.id.is_user:
                        p_tmp.setVisibility(p_tmp.INVISIBLE);
                        port.setVisibility(port.INVISIBLE);
                        MyContext.getInstance().setStatics("is_user", true);
                        break;
                    case R.id.is_admin:
                        p_tmp.setVisibility(p_tmp.VISIBLE);
                        port.setVisibility(port.VISIBLE);
                        MyContext.getInstance().setStatics("is_user", false);
                        break;
                }
            }
        });
        // 模拟一次点击事件
        RadioButton _usr = (RadioButton) findViewById(R.id.is_user);
        _usr.performClick();


    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.login_button:
                if ((boolean) LoginActivity.NetworkDetector.detect(LoginActivity.this)) {
                    if (!ip_address.getText().toString().equals("")) {
                        Toast.makeText(LoginActivity.this, "正在连接··· ···", Toast.LENGTH_SHORT).show();
                        if (!defaultIp.equalsIgnoreCase(ip_address.getText().toString()))
                            myContext.setStatics("ip", ip_address.getText().toString());
                        if ((boolean) MyContext.getInstance().getStatics("is_user")) {
                            // 用户登录，进入下一步用户界面
                            startActivity(new Intent(LoginActivity.this, ChooseActivity.class));
                        } else {
                            if (!defaultPort.equalsIgnoreCase(port.getText().toString()))
                                myContext.setStatics("port", port.getText().toString());
                            // 管理员登录，进入下一步管理员界面
                            startActivity(new Intent(LoginActivity.this, AdminActivity.class));
                        }
                    }
                } else {
                    Toast.makeText(LoginActivity.this, "网络不可用!", Toast.LENGTH_LONG).show();
                }
                break;
            case R.id.exit_button:
                finish();
                break;
        }
    }

    public static class NetworkDetector {
        public final static int NONE = 0;
        // 无网络
        public final static int WIFI = 1;
        // Wi-Fi
        public final static int MOBILE = 2;

        public static boolean detect(Activity act) {
            ConnectivityManager manager = (ConnectivityManager) act.getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);

            if (manager == null) {
                return false;
            }

            NetworkInfo networkinfo = manager.getActiveNetworkInfo();

            return networkinfo != null && networkinfo.isAvailable();
        }
    }
}